package com.skyhigh.web.webservice.services;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.skyhigh.web.webservice.domain.Carga;
import com.skyhigh.web.webservice.domain.CargaList;
import com.skyhigh.web.webservice.domain.Factura;
import com.skyhigh.web.webservice.domain.FacturaList;
import com.skyhigh.web.webservice.exceptions.ExceptionHelper;
import com.skyhigh.web.webservice.exceptions.RecordNotFoundException;
import com.skyhigh.web.webservice.exceptions.ServiceException;
import com.skyhigh.web.webservice.persistence.GenericDAO;

@Service("facturaService")
@Transactional(
    propagation = Propagation.REQUIRED,
    rollbackFor = ServiceException.class,
    isolation = Isolation.DEFAULT,
    readOnly = false)
public class FacturaServiceImpl implements FacturaService {

	@Autowired
	@Qualifier("genericDao")
	private GenericDAO dao;
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	
	
	public Factura agregar(Factura entity) {
		dao.save(entity);
        return entity;
	}

	
	public void actualizar(Factura entity) {
			
    		dao.update(entity);
    	
	}

	
	public void eliminar(Integer id) {
		try {
			dao.deleteById(Factura.class, id);
		} catch (RecordNotFoundException e) {
			throw ExceptionHelper.newPrimaryKeyNotFountEx(id);
		}
	}
	
	@Transactional(readOnly = true)
	public Factura obtenerObjeto(Integer id) {
		Factura entity = dao.<Factura>get(Factura.class, id);
		if(entity == null){
    		throw ExceptionHelper.newPrimaryKeyNotFountEx();			
		}
    	
    	return entity;
	}

	@Transactional(readOnly = true)
	public FacturaList obtenerObjetos() {
		List<Factura> entities = dao.getAll(Factura.class);    	
    	return new FacturaList(entities);
	}

	@Transactional(readOnly = true)
	public FacturaList obtenerObjetosByConsulta(String consultaSQL) {		
		List<Factura> entities = dao.find(consultaSQL);
		
		return new FacturaList(entities);
	}

	
	public int obtenerCartidadObjetos() {
		int totalObjetos = dao.count(Factura.class);
		return totalObjetos;
	}	
}
